package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 安徽网络电视台
 * @author Persh
 */
public class AhtvAnalyse implements AnalyseNews{



	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.ahtv.cn/news/vodnews/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]{8}.html",
				"http://www.ahtv.cn/v/TVM/ahtv/.*/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]{8}.html"
				,"http://www.ahtv.cn/.*/[0-9]{4}/[0-9]{4}/[0-9].*.html"
				};
		
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		video.setUrl(url);		
		String title = null;
		String desc = null;
		Long date = null;
		String tempdate = null;
		Document doc = Jsoup.parse(htmltxt);
		String updateUrl = null;
		
		if(url.contains("vodnews")){
			title = doc.select("div#liv_print_area>ul>li").get(0).text();
			tempdate=doc.select("div#liv_print_area>ul>li").get(1).text();
			/**
			 * 解析更新路径
			 */
			String staticfile = url.substring(url.indexOf("vodnews/")+"vodnews/".length());
			updateUrl = "http://www.ahtv.cn//content_stat_b.php?staticfile="+staticfile+"&showstat=1";
		}
		if(title==null || title.trim().length()==0){
			title = doc.select("h1").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("title").text();
			}
			if(title==null || title.trim().length()==0){
			}
		}
		
		desc = doc.select("li#contentarea").text();
		if(desc==null || desc.trim().length()==0){
		}
		
		if(tempdate==null || tempdate.trim().length()==0){
			String[] split = url.split("/");
			tempdate = split[split.length-1].substring(0,split[split.length-1].lastIndexOf("-")+3);
		}
		date = DateUtils.matchDate(tempdate);
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		video.setTitle(StringUtils.trimSpace(title));
		video.setContent(desc);
		video.setDate(date);
		
		
		return video;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		
		return null;
	}

	
}
